
package qldc.pm.qlktcsdldc.utils;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.servlet.http.HttpServletResponse;

import qldc.pm.qlktcsdldc.portlet.QLKTCSDLDCContants;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.repository.model.FileEntry;
import com.liferay.portal.kernel.repository.model.Folder;
import com.liferay.portal.kernel.servlet.ServletResponseConstants;
import com.liferay.portal.kernel.util.MimeTypesUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portlet.documentlibrary.DuplicateFileException;
import com.liferay.portlet.documentlibrary.FileNameException;
import com.liferay.portlet.documentlibrary.FileSizeException;
import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;

public class Utils {

	public static Date convertStringToDate(String dateInString) {

		if (dateInString == null || dateInString == "" || dateInString.length() <= 0)
			return null;
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		try {
			Date date = formatter.parse(dateInString);
			return date;
		}
		catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String formatDateToStringSQL(String dateInString) {

		if (dateInString == null || dateInString == "" || dateInString.length() <= 0)
			return null;
		if(dateInString.contains("-")){
			String[] date = dateInString.split(" ");
			String[] arr = date[0].split("-");
			return arr[2] + "/" + arr[1] + "/" + arr[0];
		}else{
			SimpleDateFormat dateFormat = new SimpleDateFormat(QLKTCSDLDCContants.DATE_PATTERN);
			return dateFormat.format(dateInString);
		}

	}

	public static Date getCurrentDate() {

		Date date = new Date();
		return date;
	}

	public static FileEntry addFileEntry(
		PortletRequest portletRequest, PortletResponse portletResponse, Folder parentFolder, File file, String name, String title) {

		ThemeDisplay themeDisplay = (ThemeDisplay) portletRequest.getAttribute(WebKeys.THEME_DISPLAY);
		HttpServletResponse httpServletResponse = PortalUtil.getHttpServletResponse(portletResponse);

		FileEntry fileEntry = null;

		try {
			fileEntry =
				DLAppLocalServiceUtil.addFileEntry(
					themeDisplay.getUserId(), parentFolder.getRepositoryId(), parentFolder.getFolderId(), name, MimeTypesUtil.getContentType(file),
					title, StringPool.BLANK, StringPool.BLANK, file, new ServiceContext());

		}
		catch (DuplicateFileException e) {
			httpServletResponse.setStatus(ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION);
		}
		catch (FileNameException e) {
			httpServletResponse.setStatus(ServletResponseConstants.SC_FILE_NAME_EXCEPTION);
		}
		catch (FileSizeException e) {
			httpServletResponse.setStatus(ServletResponseConstants.SC_FILE_SIZE_EXCEPTION);
		}
		catch (PortalException e) {
			e.printStackTrace();
		}
		catch (SystemException e) {
			e.printStackTrace();
		}
		return fileEntry;
	}

	public static Date addDay(Date date, int days) {

		// TODO you may want to check for a null date and handle it.
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, days);
		return cal.getTime();
	}

	public static Date getDateDenTuoiLamCMND() {

		Calendar cal = Calendar.getInstance();
		Date currentDate = getCurrentDate();
		cal.setTime(currentDate);
		cal.add(Calendar.YEAR, -14);
		return cal.getTime();
	}

	public static int getDiffYears(Date first, Date last) {

		Calendar a = getCalendar(first);
		Calendar b = getCalendar(last);
		int diff = b.get(Calendar.YEAR) - a.get(Calendar.YEAR);
		if (a.get(Calendar.MONTH) > b.get(Calendar.MONTH) ||
			(a.get(Calendar.MONTH) == b.get(Calendar.MONTH) && a.get(Calendar.DATE) > b.get(Calendar.DATE))) {
			diff--;
		}
		return diff;
	}

	public static Calendar getCalendar(Date date) {

		Calendar cal = Calendar.getInstance(Locale.US);
		cal.setTime(date);
		return cal;
	}
}
